package com.buddy.qreport.web.template.controller;

import com.buddy.qreport.template.IReportTemplate;
import com.buddy.qreport.template.config.TemplateManager;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/template")
@RestController("QREPORT_TemplateController")
public class TemplateController {



    @GetMapping("/getTypeAndTemplateMap")
    public ResponseEntity getTypeAndTemplateMap(){

        List<Map<String,Object>> resultList = new ArrayList<>();


        Map<String,List<IReportTemplate>> map = TemplateManager.getInstance().getTypeValueMap();

        map.forEach((k,v)->{

            Map<String,Object> tmp = new HashMap<>();

            tmp.put("name",k);
            tmp.put("value",v);

            resultList.add(tmp);
        });

        return new ResponseEntity<>(resultList , HttpStatus.OK);
    }

    @GetMapping("/getTemplate")
    public ResponseEntity getTemplate(@RequestParam("templateName") String templateName){

        return new ResponseEntity(TemplateManager.getInstance().getTemplateByName(templateName),HttpStatus.OK);
    }
}
